﻿using ExcelConfig.StoreAPI;
using ExcelConfig;
using easysmb;
using ExcelConfig.Config;
using biaofile;
using System.Text;

namespace smbtask.del
{
    internal class Program
    {
        static int Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine("Hello, World!");

            var config_path = "./config/smb.config.xlsx";
            if (!File.Exists(config_path))
            {
                if (string.IsNullOrEmpty(AppConfig.Text("验证密码")))
                {
                    Console.Write("press you config password: ");
                    var pwd = Console.ReadLine();
                    if (pwd != null && (pwd = pwd.Trim()) != string.Empty) AppConfig.WriteText("验证密码", pwd);
                    else return -1;
                }
                if (!Directory.Exists("config")) Directory.CreateDirectory("config");
                if (!File.Exists(config_path)) ConfigStoreAPI.DownloadToFile(AppConfig.Text("配置域"), "smb0001", config_path).Wait();
            }
            var cfg = new AppConfigFile("config", "smb");

            var check_path_count = (int)cfg.Number("检查目录合计");
            var check_paths = new string[check_path_count];
            for (var i = 0; i < check_path_count; i++) check_paths[i] = cfg.Text($"检查目录{i}");

            var need_delfile = new Queue<string>();

            var now = DateTime.Now.AddMinutes(-15);
            if(check_path_count > 0)
            using (var smb = new EasySMB(cfg.Text("地址")))
            {
                smb.Login(cfg.Text("账号0"), cfg.Text("密码0"));

                var queue = new Queue<EasySMBPath>();
                foreach (var path in check_paths) queue.Enqueue(smb.GetPath(path));

                while (queue.Count > 0)
                {
                    var path = queue.Dequeue();
                    if (path == null) continue;
                    //foreach (var sub_path in path.LoopPaths()) queue.Enqueue(sub_path);
                    Console.WriteLine(path.FullPath);
                    foreach (var sub_file in path.LoopFiles())
                    {
                        if (sub_file.FileExtension != "tmp") continue;
                        if (sub_file.CreationTime > now) continue; 
                        need_delfile.Enqueue(sub_file.FullPath);
                    }
                }
            }
            if (need_delfile.Count > 0)
            using (var smb = new EasySMB(cfg.Text("地址")))
            {
                smb.Login(cfg.Text("账户"), cfg.Text("密码"));

                while (need_delfile.Count > 0)
                {
                    var sub_file = smb.GetFile(need_delfile.Dequeue());

                    BiaoType sub_filetype = BiaoType.unknown;
                    using (var fs = sub_file.GetStream(FileAccess.Read, FileShare.ReadWrite)) sub_filetype = Biao.Resolve(fs);
                    switch (sub_filetype)
                    {
                        case BiaoType.ppt:
                        case BiaoType.pptx:
                        case BiaoType.xls:
                        case BiaoType.xlsx:
                        case BiaoType.doc:
                        case BiaoType.docx:
                            Console.WriteLine($"[{sub_filetype.ToString()} tmpfile]  del {sub_file.FullPath}");
                            sub_file.Delete();
                            break;
                    }
                }
            }
            return 0;
        }
    }
}
